package WWTPAgent;

import jade.content.ContentManager;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.domain.FIPANames;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.SimpleAchieveREInitiator;
import wwtp.ontology.Bypass;
import wwtp.ontology.Destination;
import wwtp.ontology.DirectSendWater;
import wwtp.ontology.Retain;
import wwtp.ontology.Water;

public class ListenerBehaviour extends Behaviour{
	
	private ContentManager manager;
	private MessageTemplate mt;
	private int state = 0;
	Water water;
	
	public ListenerBehaviour(Water water) {
		// TODO Auto-generated constructor stub
		this.water = water;
	}
	
	@Override
	public void action() {
		mt = MessageTemplate.and(
				MessageTemplate.MatchConversationId("InformHelper"),
				MessageTemplate.MatchPerformative(ACLMessage.INFORM));
		manager = myAgent.getContentManager();
		ACLMessage msg = myAgent.receive(mt);
        if (msg!=null){
    		try{
    		 	System.out.println("*** END NEGOTIATION, water from "+myAgent.getLocalName() +" will be treated by: " + msg.getContent() +" ***");
    	
    		 	Action action = null;
    		 	
    		 	
    	    	if(msg.getContent().compareTo(myAgent.getLocalName())==0){
    	    		//System.out.println("RequestForHelpBehaviour Nadie puede ayudarme, asi que retengo");
    	    		//solver.setName("Authority");
    	    		if (water.getVolume().getLitres() + ((WWTPAgent)myAgent).usedCapacity > ((WWTPAgent)myAgent).maxCapacity){
	    	    		Retain ret = new Retain();
						ret.setWater(water);
						action = new Action(myAgent.getAID(), ret);
    	    		}
    	    		else{
    	    			DirectSendWater dsw = new DirectSendWater();
						dsw.setWater(water);
						action = new Action(msg.getSender(), dsw);
    	    		}
    	    		
    	       }else{
    	    	   	Destination solver = new Destination();
    	    		solver.setName(msg.getContent());
    	    		
	    	   		//System.out.println("RequestForHelpBehaviour Me puede ayudar " + solver.getName());    	   		
    	    	   
    	    	   	Bypass bp = new Bypass();
       	    		bp.setWater(water);
	       			bp.setReceiver(solver);
	
	       			action = new Action(myAgent.getAID(), bp);
    	    	   
    	       }
     			ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
    			request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
    			request.setLanguage(((WWTPAgent) myAgent).codec.getName());
    			request.setOntology(((WWTPAgent) myAgent).ontology.getName());

    			request.addReceiver(new AID(((WWTPAgent)myAgent).dbDirectWater.getMyCloserTank(((WWTPAgent)myAgent).getLocalName()), AID.ISLOCALNAME));
    			request.setConversationId("ControlFlowProtocol-action");

    			try {
    				manager.fillContent(request, action);

    				//System.out.println(request);
    				myAgent.addBehaviour(new SimpleAchieveREInitiator(myAgent, request) {
    					protected ACLMessage prepareRequest(ACLMessage arg0) {
    						return super.prepareRequest(arg0);
    					}
    				});
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		 	state = 1;
    		}
   			catch (Exception e) {
   				e.printStackTrace();
   			}
        } else {
        	block();
        }

	}
	@Override
	public boolean done() {
		if(state == 1)return true;
		return false;
	}



}
